package com.douxg.fastCoding.web.consts;

import com.douxg.fastCoding.db.DbFunction;
import com.douxg.fastCoding.db.clickhouse.CkFunction;
import com.douxg.fastCoding.db.mysql.MysqlFunction;
import com.jfinal.plugin.activerecord.dialect.*;
import lombok.Getter;

/**
 * @Description
 * @Auther douxg
 * @Date 2020/7/14 22:12
 */
@Getter
public enum DbType {
    MYSQL("mysql", new MysqlDialect(), true, new MysqlFunction()),
    CLICKHOUSE("clickhouse", null, true, new CkFunction()),
    MARIADB("mariadb", new MysqlDialect()),
    ORACLE("oracle", new OracleDialect()),
    ORACLE_12C("oracle12c", new OracleDialect()),
    H2("h2"),
    SQLITE("sqlite", new Sqlite3Dialect()),
    SQL_SERVER2005("sqlserver2005", new SqlServerDialect()),
    SQL_SERVER("sqlserver", new SqlServerDialect()),
    POSTGRE_SQL("postgresql", new PostgreSqlDialect());
    private String name;
    private Dialect dialect;
    private Boolean isUsable = false;
    private DbFunction dbFunction;

    DbType(String name) {
        this.name = name;
    }

    DbType(String name, Dialect dialect) {
        this.name = name;
        this.dialect = dialect;
    }

    DbType(String name, Dialect dialect, Boolean isUsable, DbFunction dbFunction) {
        this.name = name;
        this.dialect = dialect;
        this.isUsable = isUsable;
        this.dbFunction = dbFunction;
    }

    public static DbType getByName(String name) {
        if (name != null) {
            for (DbType dbType : values()) {
                if (dbType.name.equals(name)) {
                    return dbType;
                }
            }
        }
        return null;
    }
}
